package com.quiz.bis.domain.vo;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 试卷JSON数据结构
 * 用于前端展示和方便缓存优化
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BisPaperJsonVo implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 单选题列表
     */
    @JsonProperty("single")
    @Builder.Default
    private List<BisPaperQuestionVo> single = new ArrayList<>();

    /**
     * 多选题列表
     */
    @JsonProperty("multiple")
    @Builder.Default
    private List<BisPaperQuestionVo> multiple = new ArrayList<>();

    /**
     * 填空题列表
     */
    @JsonProperty("fill")
    @Builder.Default
    private List<BisPaperQuestionVo> fill = new ArrayList<>();

    /**
     * 判断题列表
     */
    @JsonProperty("judge")
    @Builder.Default
    private List<BisPaperQuestionVo> judge = new ArrayList<>();

    /**
     * 问答题列表
     */
    @JsonProperty("essay")
    @Builder.Default
    private List<BisPaperQuestionVo> essay = new ArrayList<>();

    // 各题型每题分数
    @JsonProperty("singleScore")
    private Integer singleScore;

    @JsonProperty("multipleScore")
    private Integer multipleScore;

    @JsonProperty("fillScore")
    private Integer fillScore;

    @JsonProperty("judgeScore")
    private Integer judgeScore;

    @JsonProperty("essayScore")
    private Integer essayScore;

    /**
     * 添加单个题目到对应题型列表
     * @param question 题目对象
     */
    public void addQuestion(BisPaperQuestionVo question) {
        if (question == null) {
            return;
        }

        switch (question.getQuestionType()) {
            case "single":
                this.single.add(question);
                break;
            case "multiple":
                this.multiple.add(question);
                break;
            case "fill":
                this.fill.add(question);
                break;
            case "judge":
                this.judge.add(question);
                break;
            case "essay":
                this.essay.add(question);
                break;
            default:
                throw new IllegalArgumentException("未知的题目类型: " + question.getQuestionType());
        }
    }

    /**
     * 批量添加题目到对应题型列表
     * @param questions 题目列表
     */
    public void addQuestions(List<BisPaperQuestionVo> questions) {
        if (questions == null || questions.isEmpty()) {
            return;
        }

        questions.forEach(this::addQuestion);
    }

    /**
     * 获取所有题目总数
     * @return 题目总数
     */
    public int getTotalQuestionCount() {
        return single.size() + multiple.size() + fill.size() + judge.size() + essay.size();
    }

    /**
     * 清空所有题目
     */
    public void clearAllQuestions() {
        this.single.clear();
        this.multiple.clear();
        this.fill.clear();
        this.judge.clear();
        this.essay.clear();
    }
}